package com.sosee.cgfw.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.sosee.cgfw.R;
import com.sosee.cgfw.entity.Data;
import com.sosee.cgfw.entity.PiaoJiaData;
import com.sosee.cgfw.entity.ad.DataJson;
import com.sosee.cgfw.interfac.InvitationInterface;
import com.sosee.cgfw.utils.LogUtils;

import java.util.List;

/**
 * Created by Thinkpad on 2016/2/24.
 * 邀请函 adapter
 */
public class InvitationAdapter extends BaseAdapter {
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    private int status=0;//判断用于邀请函 还是 我的申请  0 邀请函  1申请
    private InvitationInterface anInterface;

    public InvitationInterface getAnInterface() {
        return anInterface;
    }

    public void setAnInterface(InvitationInterface anInterface) {
        this.anInterface = anInterface;
    }

    private List<PiaoJiaData> list;

    public List<PiaoJiaData> getList() {
        return list;
    }

    public void setList(List<PiaoJiaData> list) {
        this.list = list;
    }

    private LayoutInflater inflater;
    public InvitationAdapter(Context context){
        inflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=inflater.inflate(R.layout.item_invitation,null);
            holder=new ViewHolder();
            holder.tv_date= (TextView) convertView.findViewById(R.id.tv_date);
            holder.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_info= (TextView) convertView.findViewById(R.id.tv_info);
            holder.tv_address= (TextView) convertView.findViewById(R.id.tv_address);
            holder.tv_address_e= (TextView) convertView.findViewById(R.id.tv_address_e);
            holder.ig_accept= (ImageView) convertView.findViewById(R.id.ig_accept);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        PiaoJiaData pdata=list.get(position);
        String show=pdata.getShow().get(0);
        LogUtils.log_E("测试申请数据解析：" + show);
        DataJson date= JSONArray.parseArray(show, DataJson.class).get(0);
        holder.tv_date.setText(date.getData().getStart_date());
        holder.tv_time.setText(date.getData().getStart_time());
        holder.tv_info.setText(date.getData().getUndertake());
        holder.tv_address.setText(date.getData().getAddressA());
        holder.tv_address_e.setText(date.getData().getAddressA_e());
        String id=date.getData().getID();
        if(pdata.getStatus().equals("2")){
            convertView.setBackgroundResource(R.drawable.guanfangyaoqing);
        }else if(pdata.getStatus().equals("3")){
            convertView.setBackgroundResource(R.drawable.haoyouzhuanzeng);
        }else{
            if(position%4==0){
                convertView.setBackgroundResource(R.drawable.lv);
            }else if(position%3==0){
                convertView.setBackgroundResource(R.drawable.zi);
            }else if(position%2==0){
                convertView.setBackgroundResource(R.drawable.lan);
            }else{
                convertView.setBackgroundResource(R.drawable.fen);
            }



            if(pdata.getStatus().equals("5")){
                holder.ig_accept.setImageResource(R.drawable.shenhezhong);
                id="-1";
            }else if(pdata.getStatus().equals("6")){
                holder.ig_accept.setImageResource(R.drawable.weitongguo);

            }else{
                holder.ig_accept.setImageResource(R.drawable.yitongguo);
            }
        }
        final String showID=id;


        holder.ig_accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anInterface.onClick(showID);
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView tv_date;
        TextView tv_time;
        TextView tv_info;
        TextView tv_address;
        TextView tv_address_e;
        ImageView ig_accept;
    }
}
